from lxml import etree

text = ''' <div> <ul> 
        <li class="item-1"><a href="link1.html">first item</a></li> 
        <li class="item-1"><a href="link2.html">second item</a></li> 
        <li class="item-inactive"><a href="link3.html">third item</a></li> 
        <li class="item-1"><a href="link4.html">fourth item</a></li> 
        <li class="item-0"><a href="link5.html">fifth item</a> 
        </ul> </div> '''

# 利用etree.HTML，将字符串转化为Element对象, Element对象具有XPath的方法
html = etree.HTML(text)
print(type(html))

# 将Element对象转化为字符串
handled_html_str = etree.tostring(html).decode()
print(handled_html_str+'\n'+'-'*100)


# 提取所有item=1节点的a标签的内容
li_a_9 = html.xpath('//li[@class="item-1"]//text()')
for li_a in li_a_9:
        print(li_a) 
        print('-'*100)

#提取第一个a标签的文本
li_a_1 = html.xpath('//li[@class="item-1"][1]/a/text()')
print(li_a_1)

#提取除了第一个item=1的所有a标签
#[position()>1] 表示位置大于1的节点，排除前1个节点
li_a_other = html.xpath('//li[@class="item-1"][position()>1]/a/text()')
for li_a in li_a_other:
        print(li_a)

#获取最后一个标签文本
li_a_last = html.xpath('//li[@class="item-1"][last()]/a/text()')
print('\n'+'-'*100)
print(li_a_last)
print(type(li_a_last))
print('\n'+'-'*100)
#根据标签文本获取标签对象
li = html.xpath('//li/a[text()="fourth item"]') 
print(type(li[0]))
print(li[0].xpath('@href'))